#include "frmuserlist.h"
#include "ui_frmuserlist.h"
#include "userinfoservice.h"
#include "iconhelper.h"
#include "quihelper.h"

FrmUserlist::FrmUserlist(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FrmUserlist)
{
    ui->setupUi(this);
    this->initForm();
}

FrmUserlist::~FrmUserlist()
{
    delete ui;
}

void FrmUserlist::initForm()
{
    ui->tableView->setStyleSheet("QTableWidget::item{padding:0px;}");

    UserInfoService *userService=new UserInfoService;
    QSqlQueryModel *model= userService->getUserModel();
    model->setHeaderData(0,Qt::Horizontal,QString("用户ID"));
    model->setHeaderData(1,Qt::Horizontal,QString("用户名"));
    model->setHeaderData(2,Qt::Horizontal,QString("姓名"));
    model->setHeaderData(3,Qt::Horizontal,QString("部门"));
    model->setHeaderData(4,Qt::Horizontal,QString("用户类型"));
    ui->tableView->setModel(model);
    ui->tableView->setColumnWidth(0,200);
    ui->tableView->setColumnWidth(1,100);
    ui->tableView->setColumnWidth(2,150);
    ui->tableView->setColumnWidth(3,150);
    ui->tableView->setColumnWidth(1,100);

    //设置顶部导航按钮
    QList<int> iconMains;
    iconMains<< 0xe641 << 0xe64c<<0xe67c<<0xe64c;
    btnsLeft<<ui->btnAdd<<ui->btnEdit<<ui->btnDelete<<ui->btnWriteJson;
    foreach (QAbstractButton *btn, btnsLeft) {
        connect(btn,SIGNAL(clicked(bool)),this,SLOT(topbtnClicked()));
    }

    IconHelper::StyleColor styleColor;
    styleColor.position = "left";
    //styleColor.iconSize = 18;
    //styleColor.iconWidth = 20;
    //styleColor.iconHeight = 20;
    styleColor.borderWidth = 4;
    IconHelper::setStyle(ui->widgetTop, btnsLeft, iconMains, styleColor);
}

void FrmUserlist::updateForm()
{
    UserInfoService *userService=new UserInfoService;
    QSqlQueryModel *model= userService->getUserModel();
    ui->tableView->setModel(model);
}

void FrmUserlist::topbtnClicked()
{
    QAbstractButton *btn=(QAbstractButton *)sender();
    foreach (QAbstractButton *b, btnsLeft) {
        b->setChecked(btn==b);
    }
}

void FrmUserlist::on_btnAdd_clicked()
{
    FrmUserModify *frmModify=new FrmUserModify;
    frmModify->setWindowModality(Qt::ApplicationModal);
    connect(frmModify,SIGNAL(sendData()),this,SLOT(updateForm()));
    frmModify->show();
}

void FrmUserlist::on_btnEdit_clicked()
{
    QModelIndexList lst=ui->tableView->selectionModel()->selectedIndexes();
    if(lst.count()<=0)
    {
        QUIHelper::showMessageBox(QString("请选中要修改的数据行"));
        return;
    }
    QModelIndex curIndex=ui->tableView->currentIndex();
    int rowIndex=curIndex.row();
    QVariant userGuId=curIndex.sibling(rowIndex,0).data();
    QVariant userName=curIndex.sibling(rowIndex,1).data();
    QVariant realName=curIndex.sibling(rowIndex,2).data();
    QVariant departName=curIndex.sibling(rowIndex,3).data();
    QVariant userType=curIndex.sibling(rowIndex,4).data();


    FrmUserModify *frmModify=new FrmUserModify;
    frmModify->setUserGuid(userGuId.toString());
    frmModify->setUserName(userName.toString());
    frmModify->setRealName(realName.toString());
    frmModify->setDepartment(departName.toString());
    frmModify->setUserType(userType.toInt());
    frmModify->setWindowModality(Qt::ApplicationModal);
    connect(frmModify,SIGNAL(sendData()),this,SLOT(updateForm()));
    frmModify->show();
}

void FrmUserlist::on_btnDelete_clicked()
{
    QModelIndexList lst=ui->tableView->selectionModel()->selectedIndexes();
    if(lst.count()<=0)
    {
        QUIHelper::showMessageBox(QString("请选中要修改的数据行"));
        return;
    }
    QModelIndex curIndex=ui->tableView->currentIndex();
    int rowIndex=curIndex.row();
    QVariant userGuId=curIndex.sibling(rowIndex,0).data();

    int iResult= QUIHelper::showMessageBoxQuestion(QString("确认删除该用户？"));
    if(iResult==QMessageBox::Yes)
    {
        UserInfoService *service=new UserInfoService;
        service->del(userGuId.toString());
        updateForm();
    }
}

QString FrmUserlist::writeJson()
{
    QModelIndexList lst=ui->tableView->selectionModel()->selectedIndexes();
    if(lst.count()<=0)
    {
        QUIHelper::showMessageBox(QString("请选中要修改的数据行"));
        return QString("");
    }
    QModelIndex curIndex=ui->tableView->currentIndex();
    int rowIndex=curIndex.row();
    QVariant userGuId=curIndex.sibling(rowIndex,0).data();
    QVariant userName=curIndex.sibling(rowIndex,1).data();
    QVariant realName=curIndex.sibling(rowIndex,2).data();
    QVariant departName=curIndex.sibling(rowIndex,3).data();

    QJsonObject rootObj;
    rootObj.insert("UserGuId",userGuId.toString());
    rootObj.insert("UserName",userName.toString());
    rootObj.insert("RealName",realName.toString());
    rootObj.insert("DepartName",departName.toString());

    QJsonObject departObj;
    departObj.insert("Name","后勤部门");
    departObj.insert("Number",15);
    departObj.insert("Leader","李刚");
    rootObj.insert("Depart",departObj);

    QJsonDocument doc(rootObj);
    QByteArray json=doc.toJson();
    QString strJson=QString(json).toUtf8().data();

    return strJson;
}

QString FrmUserlist::readJson()
{
    QString strJson=writeJson();
    QJsonDocument doc=QJsonDocument::fromJson(strJson.toUtf8());
    if(!doc.isObject())
    {
        QUIHelper::showMessageBoxError(QString("错误的JSON串"));
        return QString("");
    }

    QJsonObject rootObj=doc.object();

    QString strValue;
    QStringList keys=rootObj.keys();
    for(int i=0;i<keys.length();i++)
    {
        QString key=keys[i];
        QJsonValue value=rootObj.value(key);
        if(value.isString())
        {
            strValue.append(QString("%1:%2;").arg(key).arg(value.toString()));
        }
        else if(value.isObject())
        {
            QJsonObject subObj=value.toObject();
            QString firstKey= subObj.keys().at(0);
            QString firstValue=subObj.value(firstKey).toString();
            strValue.append(QString("%1:%2;").arg(firstKey).arg(firstValue));
        }
    }

    return strValue;
}

void FrmUserlist::on_btnWriteJson_clicked()
{
    QString strJson=writeJson();
    QUIHelper::showMessageBoxInfo(strJson);
}

void FrmUserlist::on_btnReadJson_clicked()
{
    QString strValue=readJson();
    QUIHelper::showMessageBox(strValue);
}
